package com.ejia.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import com.ejia.pojo.fangkong;
import com.ejia.server.FKService;
import com.ejia.server.HouseService;
import com.ejia.server.MoneyService;
import com.jtattoo.plaf.fast.FastBorderFactory;

import javax.swing.ImageIcon;
import java.awt.Font;
import javax.swing.SpinnerListModel;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class YQFK extends JFrame {

	private JPanel contentPane;
	private static JTable table;
	private static ArrayList<fangkong> fangkongs;
	public static int id;
	public static int index;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					YQFK frame = new YQFK();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void dataBind(int zt) {
		if (zt==1 || zt==2 || zt==3) {
			fangkongs=FKService.findByLike(zt);
		}else {
			fangkongs=FKService.findby();
		}
		Object[][] data =new Object[fangkongs.size()][];
		for (int i = 0; i < data.length; i++) {
			if (fangkongs.get(i).getStatus()==1) {
				data[i]=new Object[] {
						fangkongs.get(i).getFkid(),
						fangkongs.get(i).getUserid().getRealname(),
						fangkongs.get(i).getHouseid().getAddress(),
						"<html><p style=\"background-color: green;\">绿码</p></html>",
						fangkongs.get(i).getCity(),
						fangkongs.get(i).getUpdatetime()
				};
			}else if (fangkongs.get(i).getStatus()==2) {
				data[i]=new Object[] {
						fangkongs.get(i).getFkid(),
						fangkongs.get(i).getUserid().getRealname(),
						fangkongs.get(i).getHouseid().getAddress(),
						"<html><p style=\"background-color: yellow;\">黄码</p></html>",
						fangkongs.get(i).getCity(),
						fangkongs.get(i).getUpdatetime()
				};
			}else {
				data[i]=new Object[] {
						fangkongs.get(i).getFkid(),
						fangkongs.get(i).getUserid().getRealname(),
						fangkongs.get(i).getHouseid().getAddress(),
						"<html><p style=\"background-color: red;\">红码</p></html>",
						fangkongs.get(i).getCity(),
						fangkongs.get(i).getUpdatetime()
				};
			}
		}
		table.setModel(new DefaultTableModel(
				data,
				new String[] {
					"\u7F16\u53F7", "\u59D3\u540D", "\u4F4F\u5740", "\u9632\u75AB\u72B6\u6001", "\u884C\u7A0B\u7801", "\u767B\u8BB0\u65F6\u95F4"
				}
			));
		table.getColumnModel().getColumn(2).setPreferredWidth(300);
		table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 26));
	}
	
	/**
	 * Create the frame.
	 */
	public YQFK() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(YQFK.class.getResource("/com/ejia/photo/客户管理系统.png")));
		setTitle("E家物业-疫情防控记录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1269, 507);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("添加");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				new YQFKadd().setVisible(true);
				dispose();
			}
		});
		btnNewButton.setFont(new Font("Dialog", Font.PLAIN, 32));
		btnNewButton.setIcon(new ImageIcon(YQFK.class.getResource("/com/ejia/photo/添加.png")));
		btnNewButton.setBounds(435, 16, 168, 76);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("编辑");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (table.getSelectedRowCount()>0) {
					index= table.getSelectedRow();
					id=fangkongs.get(index).getFkid();
					new YQFKBJ().setVisible(true);
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "请选择数据");
				}
			}
		});
		button.setFont(new Font("Dialog", Font.PLAIN, 32));
		button.setIcon(new ImageIcon(YQFK.class.getResource("/com/ejia/photo/编辑.png")));
		button.setBounds(234, 16, 168, 76);
		contentPane.add(button);
		
		JButton button_1 = new JButton("刷新");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dataBind(0);
			}
		});
		button_1.setFont(new Font("宋体", Font.PLAIN, 32));
		button_1.setIcon(new ImageIcon(YQFK.class.getResource("/com/ejia/photo/刷新.png")));
		button_1.setBounds(636, 16, 168, 76);
		contentPane.add(button_1);
		
		JSpinner spinner = new JSpinner();
		spinner.setFont(new Font("宋体", Font.PLAIN, 32));
		spinner.setModel(new SpinnerListModel(new String[] {"\u7EFF\u7801", "\u9EC4\u7801", "\u7EA2\u7801"}));
		
		
		spinner.setBounds(837, 16, 175, 76);
		contentPane.add(spinner);
		
		JButton button_2 = new JButton("查询");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (spinner.getValue().equals("\u9EC4\u7801")) {
					dataBind(2);
				}else if (spinner.getValue().equals("\u7EA2\u7801")){
					dataBind(3);
				}else {
					dataBind(1);
				}
			}
		});
		button_2.setIcon(new ImageIcon(YQFK.class.getResource("/com/ejia/photo/查询.png")));
		button_2.setFont(new Font("宋体", Font.PLAIN, 32));
		button_2.setBounds(1045, 16, 168, 76);
		contentPane.add(button_2);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(15, 107, 1217, 329);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("宋体", Font.PLAIN, 22));
		table.setRowHeight(30);
		dataBind(0);
		scrollPane.setViewportView(table);
		
		JButton button_3 = new JButton("返回");
		button_3.setIcon(new ImageIcon(YQFK.class.getResource("/com/ejia/photo/返回 (1).png")));
		button_3.setFont(new Font("Dialog", Font.PLAIN, 32));
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new Main().setVisible(true);
				dispose();
			}
		});
		button_3.setBounds(33, 16, 168, 76);
		contentPane.add(button_3);
		setLocationRelativeTo(null);
	}
}
